package com.srb.common.exception;

import com.srb.common.result.ResponseEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 自定义一个运行时异常类,它不是检查异常类
 * 作用：使用一个或较少的异常类,可以捕获和显示所有异常信息。
 * 在程序中抛出这个异常,并在统一异常处理器中获取自定义异常对象。
 * -- 在枚举中添加需要捕获的异常的code和message,抛出去后就会被统一异常处理器处理的
 */
@Data
@NoArgsConstructor
public class BusinessException extends RuntimeException  {

    private Integer code;
    private String message;

    public BusinessException(String message) {
        this.message = message;
    }

    public BusinessException(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public BusinessException(Integer code, String message,Throwable cause) {
        super(cause);
        this.code = code;
        this.message = message;
    }

    public BusinessException(ResponseEnum responseEnum) {
        this.message = responseEnum.getMessage();
        this.code = responseEnum.getCode();
    }

    public BusinessException(ResponseEnum responseEnum,Throwable cause) {
        super(cause);
        this.message = responseEnum.getMessage();
        this.code = responseEnum.getCode();
    }
}
